
const app = getApp()

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		title:'',
		name:'',
		phone:'',
		cityPicker: [],
		cityCode: [],
		districtPicker: [],
		districtCode: [],
		siteId: 340100,
		index: 0,
		index2: 0,
		index3: 3,
		aId: 0,
		houseId: 0,
		buildArea: '',
		areaName: '',
		siteName: '',
		price: '',
		isSubmit: true,
		areaPicker: [
			'50㎡以下',
			'50㎡-70㎡',
			'70㎡-90㎡',
			'90㎡-120㎡',
			'120㎡-150㎡',
			'150㎡-200㎡',
			'200㎡-300㎡',
			'300㎡以上',
		]
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		wx.showShareMenu({
			withShareTicket: true
		})

		if (options.scene != undefined && options.scene != '') {
			let scene = decodeURIComponent(options.scene);
		} else {
			if (options.title != undefined) {
				this.setData({
					title: options.title,
					houseId: options.houseId
				})
			}
		}

		let cityCode = wx.getStorageSync('cityCode')
		if (cityCode != undefined && cityCode != '') {
			this.setData({
				siteId: cityCode
			})
		}

		this.getCity()
		this.getDistrict()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (res) {
		if (res.from === 'button') {
			// 来自页面内转发按钮
			console.log(res.target)
		}
		return {
			title: '复工了，我在星房惠预约了看房团，你也来预约吧！',
			path: '/pages/kanfangtuan/index'
		}
	},

	handleInput(e) {
		const name = e.target.dataset.name
		let val = []
		val[name] = e.detail.value
		this.setData(val)
	},

	handleSubmit() {
		let vm = this;
		let title = vm.data.title;
		let phone = vm.data.phone;
		let name = vm.data.name
		let buildArea = vm.data.buildArea

		if (!(/^[\u4E00-\u9FA5]{2,4}$/.test(name))) {
			wx.showToast({
				title: '姓名格式错误！请重新输入',
				icon: 'none',
				duration: 2000
			});
			return false;
		} else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))) {
			wx.showToast({
				title: '手机号码格式错误！请重新输入',
				icon: 'none',
				duration: 2000
			});
			return false;
		} else {

			let open_id = wx.getStorageSync('user_new')

			if  (open_id == undefined || open_id == '') {
				wx.showToast({
					title: '请先登录',
					duration: 1000,
					icon: 'none'
				})

				wx.reLaunch({
					url: '/pages/member/index?redirect=' + encodeURIComponent('/pages/kanfangtuan/index')
				})

				return false;
			}

			let data = {}
			data['open_id'] = open_id
			data['title'] = title
			data['name'] = name
			data['buildArea'] = this.data.areaPicker[this.data.index3]
			data['siteId'] = this.data.siteId
			data['aId'] = this.data.aId
			data['houseId'] = this.data.houseId
			data['areaName'] = this.data.areaName
			data['siteName'] = this.data.siteName
			data['price'] = this.data.price
			data['phone'] = this.data.phone

			console.log(data)

			app.core('post', 'newhouse/kanfangtuan', {...data}, (res) => {
					console.log(res)
					if (res.resultCode == 200) {
						wx.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 2000
						});

						vm.setData({
							isSubmit: false
						})
						setTimeout(function () {
							wx.navigateBack({
								delta: 1
							})
						}, 1000)
					} else {
						wx.showToast({
							title: '提交失败！' + res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				},
				(err) => {
					console.log(err)
				}
			)

		}

	},

	//获取城市
	getCity: function() {
		// wx.showLoading({
		//   title: '加载中',
		// })

		let vm = this;
		let siteId = this.data.siteId
		app.core('get', 'get_all_sites', {}, (res) => {
				var picker = vm.data.picker;
				var citylist = [];
				var showcitylist = [];
				for (var i = 0; i < res.data.length; i++) {
					citylist.push({
						name: res.data[i].area,
						id: res.data[i].id
					});
					showcitylist.push(res.data[i].area);

					if (siteId > 0 && res.data[i].id == siteId) {
						vm.setData({
							index: i,
							siteId: res.data[i].id,
							siteName: res.data[i].area,
						})
					}
				}

				vm.setData({
					cityPicker: showcitylist ,
					cityCode: citylist,
				});
				// wx.hideLoading()
			},
			(err) => {
				console.log(err)
			}
		)
	},

	handleCityPicker: function (e) {
		this.setData({
			index: e.detail.value,
			siteId: this.data.cityCode[e.detail.value]['id'],
			siteName: this.data.cityCode[e.detail.value]['name'],
		})
		this.getDistrict()
	},

	handleDistrictPicker: function (e) {
		this.setData({
			index2: e.detail.value,
			aId: this.data.districtCode[e.detail.value]['id'],
			areaName: this.data.cityCode[e.detail.value]['name'],
		})
	},

	handleAreaPicker: function (e) {
		this.setData({
			index3: e.detail.value,
		})
	},

	getDistrict: function(e) {
		let vm = this;
		let siteId = this.data.siteId
		app.core('get', 'district?siteId=' + siteId, {}, (res) => {
				var citylist = [];
				var showcitylist = [];
				for (var i = 0; i < res.data.length; i++) {
					citylist.push({
						name: res.data[i]['areaName'],
						id:res.data[i]['id'],
					});
					showcitylist.push(res.data[i]['areaName']);

					if (vm.data.aId == 0) {
						console.log(res.data[i]['id'])
						vm.setData({
							aId: res.data[i]['id'],
							areaName: res.data[i]['areaName'],
						});
					}
				}

				vm.setData({
					districtPicker: showcitylist ,
					districtCode: citylist,
				});
				// wx.hideLoading()
			},
			(err) => {
				console.log(err)
			}
		)
	},
})
